//
//  CHSearchViewController.m
//  CarFile
//
//  Created by 陈浩 on 2018/1/26.
//  Copyright © 2018年 陈浩. All rights reserved.
//

#import "CHSearchViewController.h"
#import "CHReservationDetailController.h"
#import "CHYYGLTableViewCell.h"
#import "CHTextField.h"
#import "CHYYGLModel.h"
@interface CHSearchViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) CHTextField *searchField;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) NSInteger index;   //  页数
@end

@implementation CHSearchViewController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupSearchView];
    
    [self setupTableView];
    
    
}

-(void)setupSearchView{
    
    CHTextField *searchField = [[CHTextField alloc] initWithFrame:CGRectMake(0, 0, 280*ScaleX, 30)];
    searchField.layer.cornerRadius = 5;
    searchField.layer.masksToBounds = YES;
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"输入企业名称搜索" attributes:
                                      @{NSForegroundColorAttributeName:[UIColor whiteColor]
                                        }];
    searchField.textField.textColor = [UIColor whiteColor];
    searchField.textField.attributedPlaceholder = attrString;
    searchField.searchImgName = @"home_yygl_search";
    searchField.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    self.navigationItem.titleView = searchField;
    self.searchField = searchField;
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30*ScaleX, 13)];
    btn.titleLabel.font = Font(14);
    [btn setTitle:@"搜索" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchResult) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}

//  建立列表UI
-(void)setupTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH  -64) style:UITableViewStylePlain];
    // 设置tableView的基本信息
    
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    // 下拉刷新
    _tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getNetworkData];
        
    }];
    //        //     上拉刷新
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    _tableView.mj_footer.automaticallyHidden = YES;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}


-(void)searchResult{
    
    [MBProgressHUD showMessage:@"加载中..."];
    [self getNetworkData];
    
    
}



#pragma mark -- 网络请求数据
-(void)getNetworkData{
    _index = 1;
    [self.tableView.mj_footer endRefreshing];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = (NSString *)[Util getValue:@"areaid"];
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"name"] = self.searchField.textField.text;
    params[@"user"] = [Globle getInstance].user;
    params[@"state"] = @"";
    params[@"begintime"] = @"";
    params[@"endtime"] = @"";
    params[@"pagenum"] = [NSString stringWithFormat:@"%zd",_index];
    params[@"pagesize"] = @"10";
 
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovsearchorder" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [MBProgressHUD hideHUD];
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHYYGLModel *model = [CHYYGLModel objectWithKeyValues:dict];
                
                [self.datas addObject:model];
            }
            if (self.datas.count < 15) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            if (self.datas.count == 0) {
                
                self.tableView.placeholder = @"没有查询企业记录";
                self.tableView.placeholderImage = @"data_null";
            }
            
            
        }else{
            
            self.tableView.placeholder = @"没有查询到企业记录";
            self.tableView.placeholderImage = @"data_null";
            
        }
        
        [self.tableView reloadData];
        
    }];
    
}

#pragma mark --  加载更多数据
-(void)loadMoreData{
    _index++;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"areaid"] = (NSString *)[Util getValue:@"areaid"];
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    
    params[@"user"] = [Globle getInstance].user;
    params[@"state"] = @"";
    params[@"begintime"] = @"";
    params[@"endtime"] = @"";
    params[@"pagenum"] = [NSString stringWithFormat:@"%zd",_index];
    params[@"pagesize"] = @"15";
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovsearchorder" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [self.tableView.mj_footer endRefreshing];
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            NSMutableArray *newDatas = [NSMutableArray array];
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHYYGLModel *model = [CHYYGLModel objectWithKeyValues:dict];
                
                [newDatas addObject:model];
            }
            if (newDatas.count < 15) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.datas addObjectsFromArray:newDatas];
            
        }else{
            
            self.tableView.placeholder = @"没有查询到预约记录";
            self.tableView.placeholderImage = @"data_null";
            
        }
        
        [self.tableView reloadData];
        
    }];
}



#pragma mark - UITableView数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHYYGLTableViewCell *cell = [CHYYGLTableViewCell cellForTableView:tableView];
    cell.model = self.datas[indexPath.section];
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor =KRGB(230, 243, 252);
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 230;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    //    跳转预约详情界面
    CHReservationDetailController *vc = [[CHReservationDetailController alloc] init];
    vc.model = self.datas[indexPath.section];
    [self.navigationController pushViewController:vc animated:YES];
    
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
